﻿using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPN.PJN.Silverlight.Extensions.Converters
{
    /// <summary>
    /// Convertit un tableau de bytes en Image
    /// </summary>
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is byte[])
            {
                var bytedata = (byte[])value;
                BitmapImage img = new BitmapImage();
                MemoryStream stream = new MemoryStream();

                stream.Write(bytedata, 0, bytedata.Length);
                try
                {
                    img.SetSource(stream);
                }
                catch
                {
                    // Rien ...
                }

                return (ImageSource)img;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
